﻿using SanJ.FreeCare.ObservationDefinitionItems;
using SanJ.FreeCare.ConditionDefinitions;
using SanJ.FreeCare.Peis.Configure.Disease;
using SanJ.FreeCare.ValueSets;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;

namespace SanJ.FreeCare.Peis
{
    public partial class PeisAppService : FreeCareAppService, IPeisAppService
    {
        public async Task<PagedResultDto<DiseaseDto>> GetDiseaseListAsync(GetDiseaseInput input)
        {
            var totalCount = await _conditionDefinitionManager.GetCountAsync(input.FilterText);
            var items = await _conditionDefinitionManager.GetPageLisAsync(input.FilterText, input.Sorting, input.MaxResultCount, input.SkipCount);
            List<DiseaseDto> resultItems = new List<DiseaseDto>();
            return new PagedResultDto<DiseaseDto>
            {
                TotalCount = totalCount,
                Items = resultItems
            };
        }
        public async Task<DiseaseDto> GetDiseaseAsync(Guid id)
        {
            var cd = await _conditionDefinitionManager.GetByIdAsync(id);
            return ToDiseaseDto(cd);
        }
        public async Task<DiseaseDto> CreateDiseaseAsync(DiseaseCreateDto input)
        {
            ConditionDefinition cd = ToConditionDefinition(input);
            var insertedEntity = await _conditionDefinitionManager.CreateAsync(cd);
            DiseaseDto result = ToDiseaseDto(insertedEntity);
            return result;
        }
        public async Task<DiseaseDto> UpdateDiseaseAsync(DiseaseUpdateDto input)
        {
            ConditionDefinition cd = ToConditionDefinition(input);
            var updatedEntity = await _conditionDefinitionManager.UpdateAsync(input.Id, cd);
            var result = ToDiseaseDto(updatedEntity);
            return result;
        }
        public Task DeleteDiseaseAsync(Guid id)
        {
            return _conditionDefinitionManager.Delete(id);
        }
        private ConditionDefinition ToConditionDefinition(DiseaseCreateDto input)
        {
            ConditionDefinition cd = new ConditionDefinition();
            cd.Name = input.PortName;
            cd.Spell = input.Spell;
            string[] dateTypes = new string[1];
            dateTypes[0] = input.DataType;
            cd.PermittedDataType = JsonSerializer.Serialize(dateTypes);
            string[] units = new string[1];
            units[0] = input.UnitCode;
            cd.PermittedUnit = JsonSerializer.Serialize(units);
            cd.Code = input.PortCode;
            cd.Order = input.Order;
            cd.Description = input.Description;
            cd.Note = input.Note;
            cd.Classification = input.Classification;
            cd.ClassificationOder = input.ClassificationOder;
            cd.Gender = input.Gender;
            cd.SuifangCycle = input.SuifangCycle;
            cd.Notification = input.Notification;
            cd.CommonCause = input.CommonCause;
            cd.MedicalInterpretation = input.MedicalInterpretation;
            cd.DtSuggest = input.DtSuggest;
            return cd;
        }
        private DiseaseDto ToDiseaseDto(ConditionDefinition source)
        {
            DiseaseDto result = new DiseaseDto();
            result.DiseaseItems = new List<DiseaseItemDto>();
            result.Id = source.Id;
            result.Name = source.Name;
            result.Code = source.Code;
            result.CommonCause = source.CommonCause;
            result.MedicalInterpretation = source.MedicalInterpretation;
            result.DtSuggest = source.DtSuggest;
            result.Description = source.Description;
            result.Order = source.Order;
            result.Note = source.Note;

            return result;
        }
    }
}
